/*
 * HttpPost.cpp
 *
 *  Created on: 2010-7-15
 *      Author: zhzuo
 */

#include "HttpPost.h"
using namespace std;

HttpPost::HttpPost(QObject *parent) : QObject(parent) {
    http = new QHttp(this);
    connect(http, SIGNAL(done(bool)), this, SLOT(done(bool)));
}

void HttpPost::downloadFile(const QUrl &url,QString data) {
    buffer=new QBuffer();
    http->setHost(url.host(), url.port(80));
    http->post(url.host(),data.toUtf8(),this->buffer);
    http->close();
}

void HttpPost::downloadFile(const QUrl &url,QByteArray* data) {
    buffer=new QBuffer();
    http->setHost(url.host(), url.port(80));
    http->post(url.host(),*data,this->buffer);
    http->close();
}

QString HttpPost::getString()
{
	QByteArray bytes;
	bytes = this->buffer->buffer();
	QString string(bytes);
	this->buffer->close();
	return string;
}

void HttpPost::done(bool error) {
    if (error) {
        cerr << "Error: " << qPrintable(http->errorString()) << endl;
    } else {
        cerr << "File downloaded as " << qPrintable(http->errorString())
             << endl;
    }
    emit finished();
}
